package com.syq.web;

import com.syq.bean.Menu;
import com.syq.bean.Users;
import com.syq.service.MenuService;
import com.syq.service.impl.MenuServiceImpl;
import com.syq.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/power/menu/menus")
public class MenuServlet extends HttpServlet {
    MenuService menuService = new MenuServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("select".equals(method)) {
            select(req,resp);
        } else if ("info".equals(method)) {
            getInfo(req,resp);
        }
    }

    protected void getInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数
        String menuId = req.getParameter("menuId");
        Menu menu = menuService.getMenuById(Integer.parseInt(menuId));
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menuList",menuList);
        req.setAttribute("menu",menu);
        req.getRequestDispatcher("info.jsp").forward(req,resp);
    }
        protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数
        String index = req.getParameter("index");
        System.out.println("intdex:"+index);
        int pageIndex = (index==null||index.length()==0)?1:Integer.parseInt(index);
        //查询数据列表
        PageUtil pageUtil = new PageUtil();
        pageUtil.setPageIndex(pageIndex);
        List<Menu> usersList = menuService.getMenuList(pageIndex, pageUtil.getPageSize());

        //总条数
        int total = menuService.total();
        pageUtil.setTotal(total);
        pageUtil.setDataList(usersList);
        //重定向
        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }
}
